﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using myBoard.Framework;
using myComics.Model;
using myComics.BO;
using System.Windows.Threading;
using System.Diagnostics;
using System.Reflection;

namespace myComics
{
	/// <summary>
    /// Interaction logic for myComicsMagnet.xaml
	/// </summary>
	public partial class myComicsMagnet : IMagnet , IFlip
	{
        private Comics _comics;
        private DispatcherTimer _timer;

        public myComicsMagnet()
		{
			this.InitializeComponent();
            _comics = this.Resources["ComicsDataSource"] as Comics;
            _nextStripBt.Click += new RoutedEventHandler(_nextStripBt_Click);
            _previousStripBt.Click += new RoutedEventHandler(_previousStripBt_Click);
            _comicsCbx.SelectionChanged += new SelectionChangedEventHandler(_comicsCbx_SelectionChanged);
            _comicsCbx.Loaded += new RoutedEventHandler(_comicsCbx_Loaded);
            _timer = new DispatcherTimer();
            _timer.Interval = new TimeSpan(1, 0, 0);
            _timer.Tick += new EventHandler(timer_Tick);
            _flipBt.Click += new RoutedEventHandler(_flipBt_Click);
            _okBt.Click += new RoutedEventHandler(_okBt_Click);
            _goComicsLogo.MouseLeftButtonUp += new MouseButtonEventHandler(_goComicsLogo_MouseLeftButtonUp);
            _linkBt.MouseLeftButtonUp += new MouseButtonEventHandler(_linkBt_MouseLeftButtonUp);
		}

        void _linkBt_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Process.Start(((Comic)_comicsCbx.SelectedItem).CurrentUrl);
            if (CloseMyBoardRequested != null)
                CloseMyBoardRequested(this, null);
        }

        void _goComicsLogo_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            Process.Start("http://www.gocomics.com/");
            if (CloseMyBoardRequested != null)
                CloseMyBoardRequested(this, null);
        }

        void _okBt_Click(object sender, RoutedEventArgs e)
        {
            if (FlipRequested != null)
                FlipRequested(MagnetSides.Front);
        }

        void _flipBt_Click(object sender, RoutedEventArgs e)
        {
            if (FlipRequested != null)
                FlipRequested(MagnetSides.Back);
        }

        void timer_Tick(object sender, EventArgs e)
        {
            ((Comic)_comicsCbx.SelectedItem).Refresh();
        }

        void _comicsCbx_Loaded(object sender, RoutedEventArgs e)
        {
            ((Comic)_comicsCbx.SelectedItem).Refresh();
        }

        void _comicsCbx_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            ((Comic)_comicsCbx.SelectedItem).Refresh();
        }

        void _previousStripBt_Click(object sender, RoutedEventArgs e)
        {
            ((Comic)_comicsCbx.SelectedItem).Previous();
        }

        void _nextStripBt_Click(object sender, RoutedEventArgs e)
        {
            ((Comic)_comicsCbx.SelectedItem).Next();
        }

        #region IMagnet Members

        public string StoreID
        {
            get
            {
                return "{2710BE37-5451-48d3-8FA6-05DD5C23ECB5}";
            }
        }

        public event EventHandler CloseMyBoardRequested;

        public event OnFlipRequested FlipRequested;

        public Panel GetBackSide()
        {
            return _back;
        }

        public Panel GetFlipZone()
        {
            return _flipZone;
        }

        public Panel GetFrontSide()
        {
            return _front;
        }

        public BitmapImage Icon
        {
            get
            {
                return ResourcesAccess.GetBitmapImageFromResource(Assembly.GetExecutingAssembly(), "myComics.Images.Icon.png");
            }
        }

        BitmapImage _thumbnail;

        public BitmapImage Thumbnail
        {
            get
            {
                return ResourcesAccess.GetBitmapImageFromResource(Assembly.GetExecutingAssembly(), "myComics.Images.Thumbnail.png");
            }
        }

        public string MagnetName
        {
            get { return "myComics"; }
        }

        public void Terminate()
        {
            _timer.Stop();
            _timer = null;
        }

        #endregion
    }
}